<?php
class adminGameController extends base
{

    public function __construct()
    {
        parent::__construct('admin');
        $this->load('admin');
        
        // 判断权限
        if(empty($this->realUser['uid']) || !$this->load('adminList')->isAdminByHupuUid($this->realUser['uid']))
        {
            echo 'jrs, 不要四处乱看哦～～';
            exit();
        }
    }
    
    /*
     * 游戏开关
     */
    public function dogameSwitch(){
        $is_show = common::request('is_show','R');  //1.开启，0.关闭
        $start_time = common::request('start_time','R');
        $end_time = common::request('end_time','R');

        if($start_time && $end_time){
            $gameConf = array(
                'is_show' => $is_show,
                'start_time' => $start_time,
                'end_time' => $end_time
            );
            $this->redis->set(REDIS_PREFIX . 'gameIsOpen', json_encode($gameConf));
        }
        $gameInit = $this->redis->get(REDIS_PREFIX . 'gameIsOpen');
        if($gameInit){
            $gameInit = json_decode($gameInit, true);
        }else{
            $gameInit = array('is_show' => '', 'start_time' => '', 'end_time' => '');
        }
        
        $this->view->assign('gameInit', $gameInit);  //用户参与情况
        $this->view->display('game/gameSwitch');
    }
 
    /**
    * 奖品列表
    */
    public function dogiftInfo()
    {
        $p = common::request('p', 'R', 1);   //页码
        $giftType = common::request('giftType', 'R');   //奖品类型(1:实物;2:虚拟)
        
        $countSql = "select count(1) from r_game_gifts where is_show = 0";
        !empty($giftType) && $countSql .= " and gift_type = {$giftType}";
        $num = $this->db->getOne($countSql);

        $search = compact('giftType');
        //分页
        $offset = 20;
        $parameter = "&giftType=".$giftType;
        require FILE . 'lib/page.class.php';
        $page = new Page($num, $offset, $parameter);
        $pageStr = $page->show();
        $start = ($p - 1) * $offset;

        $sql = "select * from r_game_gifts where is_show = 0";
        !empty($giftType) && $sql .= " and gift_type = {$giftType}";
        $sql .= " limit $start,$offset";

        $list = $this->db->getAll($sql);
        if($list){
            foreach ($list as &$v) {
                $v['gift_type'] == 1 && $v['type'] = '实物';
                $v['gift_type'] == 2 && $v['type'] = '虚拟';
                $v['chance'] = floatval($v['chance']);
                $v['channel'] == 0 && $v['channelName'] = 'app内';
                $v['channel'] == 1 && $v['channelName'] = '微信中';
            }
        }
        $this->view->assign('pageStr', $pageStr);
        $this->view->assign('search', $search);  //搜索条件
        $this->view->assign('list', $list);  
        $this->view->display('game/giftInfo');
    }

    /**
    * 配置奖品
    */
    public function dogiftUpdate()
    {
        $gid = intval(common::request('gid', 'R'));

        //根据id获取奖品信息
        $prizeInfo = array();
        $isNew = false;
        if($gid){    //update
            $isNew = true;
            $prizeInfo = $this->db->getRowTable('r_game_gifts', array(), "gid = {$gid}");
            $prizeInfo['chance'] = floatval($prizeInfo['chance']);
        }

        if(common::method() == 'POST')
        {   
            $name = common::request('name', 'P');
            $img = $this->load('qiniu')->uploadQiNiuImg('img', md5(time().'gameGiftImage'));
            $img = $img ? $img : common::request('keepGiftImg','R');
            $total = common::request('total', 'P');
            $chance = common::request('chance', 'P');
            $isInfinite = common::request('isInfinite', 'P');
            $gift_type = common::request('gift_type', 'P');
            $channel = common::request('channel', 'P');
            $is_show = common::request('is_show', 'P');

            $params = compact('name','img','total','chance','isInfinite','gift_type','channel', 'is_show');

            if($gid){   //update
                $params['gid'] = $gid;
                $this->db->updateRowTable('r_game_gifts', $params);
            }else{
                $params['remain'] = $total;
                $addtime = time();
                $this->db->insertRowTable('r_game_gifts', $params);
            }
            common::go("http://".$this->config['host'].'/adminGame/giftInfo');
        }else{
            !$prizeInfo && $prizeInfo = array('gid' => '', 'name' => '', 'img' => '', 'total' => '', 'chance' => '', 'gift_type' => '', 'channel' => '', 'isInfinite' => 0, 'is_show' => 0);

            $this->view->assign('isNew', $isNew);
            $this->view->assign('prizeInfo', $prizeInfo);
            $this->view->display('game/giftUpdate');
        }
    }

    /**
    * 用户中奖信息
    */
    public function douserWin()
    {
        $p = common::request('p', 'R', 1);   //页码
        $uid = common::request('uid', 'R');   
        
        $countSql = "select count(1) from r_game_gift_record where 1=1 ";
        !empty($uid) && $countSql .= " and uid = {$uid}";
        $num = $this->db->getOne($countSql);

        $search = compact('uid');
        //分页
        $offset = 20;
        $parameter = "&uid=".$uid;
        require FILE . 'lib/page.class.php';
        $page = new Page($num, $offset, $parameter);
        $pageStr = $page->show();
        $start = ($p - 1) * $offset;

        $sql = "select w.*,p.name from r_game_gift_record w LEFT JOIN r_game_gifts p ON p.gid = w.gift_id where 1=1 ";
        !empty($uid) && $sql .= " and uid = {$uid}";
        $sql .= " order by addtime desc limit $start,$offset";

        $list = $this->db->getAll($sql);
        if($list){
            foreach ($list as &$v) {
                $v['pCity'] = $v['province'] .'-'. $v['city'];
                !empty($v['district']) && $v['pCity'] .= '-'. $v['district'];
                $v['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
                $v['is_delivery'] == 0 && $v['delivery'] = "未填写地址";
                $v['is_delivery'] == 1 && $v['delivery'] = "未发货";
                $v['is_delivery'] == 2 && $v['delivery'] = "已发货";
                $v['channel'] == 0 && $v['channelName'] = "app内";
                $v['channel'] == 1 && $v['channelName'] = "微信中";
            }
        }

        $this->view->assign('pageStr', $pageStr);
        $this->view->assign('search', $search);  //搜索条件
        $this->view->assign('list', $list);  
        $this->view->display('game/userWin');
    }

    /**
    * 更改发货状态
    */
    public function doupdateGiftStatus()
    {
        $id = intval(common::request('id', 'R'));
        $is_delivery = intval(common::request('is_delivery', 'R'));   
        
        if(common::method() == 'POST')
        {   
            $params['id'] = intval(common::request('id', 'P'));
            $params['is_delivery'] =  common::request('is_delivery', 'P');
            $this->db->updateRowTable('r_game_gift_record', $params);
            common::go("http://".$this->config['host'].'/adminGame/userWin');
        }
        $this->view->assign('is_delivery', $is_delivery);
        $this->view->assign('id', $id);
        $this->view->display('game/giftRecordUpdate');
    }

    /**
    * 小宠物列表
    */
    public function domonstersList()
    {   
        $p = common::request('p', 'R', 1);   //页码
        $uid = common::request('uid', 'R');   
        
        $countSql = "select count(1) from r_game_monsters where 1=1 ";
        !empty($uid) && $countSql .= " and uid = {$uid}";
        $num = $this->db->getOne($countSql);

        $search = compact('uid');
        //分页
        $offset = 20;
        $parameter = "&uid=".$uid;
        require FILE . 'lib/page.class.php';
        $page = new Page($num, $offset, $parameter);
        $pageStr = $page->show();
        $start = ($p - 1) * $offset;

        $sql = "select * from r_game_monsters where 1=1 ";
        !empty($uid) && $sql .= " and uid = {$uid}";
        $sql .= " order by points desc limit $start,$offset";

        $list = $this->db->getAll($sql);
        if($list){
            foreach ($list as &$v) {
                $v['level'] = $this->load('game')->getLevelByExperience($v['experience']);
            }
        }

        $this->view->assign('pageStr', $pageStr);
        $this->view->assign('search', $search);  //搜索条件
        $this->view->assign('list', $list);  
        $this->view->display('game/monstersList');
    }

    /**
    * 饲养者列表
    */
    public function domasterList()
    {   
        $p = common::request('p', 'R', 1);   //页码
        $uid = common::request('uid', 'R'); 
        $city = common::request('city', 'R');  
        $sort = common::request('sort','R','id');
        $order = common::request('order','R','DESC');
        
        $countSql = "select count(1) from r_game_users where 1=1 ";
        !empty($uid) && $countSql .= " and uid = {$uid}";
        !empty($city) && $countSql .= " and city = \"{$city}\"";
        $num = $this->db->getOne($countSql);

        $search = compact('uid','city','sort','order');
        //分页
        $offset = 20;
        $parameter = "&uid=".$uid."&city=".$city."&sort=".$sort."&order=".$order;
        require FILE . 'lib/page.class.php';
        $page = new Page($num, $offset, $parameter);
        $pageStr = $page->show();
        $start = ($p - 1) * $offset;

        $sql = "select * from r_game_users where 1=1 ";
        !empty($uid) && $sql .= " and uid = {$uid}";
        !empty($city) && $sql .= " and city = \"{$city}\"";
        if(in_array($sort, array('poi_count','gift_count','given_buff_count','cared_count'))){
            $sql .= " order by {$sort} {$order}";
        }
        $sql .= " limit $start,$offset";

        $list = $this->db->getAll($sql);
        if($list){
            foreach ($list as &$v) {
                $v['nickname'] = $this->redis->hget(REDIS_PREFIX . 'UserInfo_' . $v['uid'], 'nickname');
                //获取POI交互次数
                $v['onlyPoiCount'] = $this->db->getOne("SELECT count(1) FROM r_game_fight_record where uid = {$v['uid']}");
                //获取照料人数
                $v['onlyCareCount'] = $this->db->getOne("SELECT count(1) FROM (SELECT * FROM r_game_care_record_wx where to_uid = {$v['uid']} GROUP BY from_openid) a");
                //获取助力人数
                $v['onlyBuffCount'] = $this->db->getOne("SELECT count(1) FROM (SELECT * FROM r_game_buff_record_wx where to_uid = {$v['uid']} GROUP BY from_openid) a");
            }
        }
        if(in_array($sort, array('onlyPoiCount','onlyCareCount','onlyBuffCount'))){
            $arrSort = array();
            foreach($list as $uniqid => $row){  
                foreach($row as $key=>$value){  
                    $arrSort[$key][$uniqid] = $value;  
                }  
            }  
            $order == 'asc' && $direction = "SORT_ASC";
            $order == 'desc' && $direction = "SORT_DESC";
            array_multisort($arrSort[$sort], constant($direction), $list);  
        }

        $this->view->assign('pageStr', $pageStr);
        $this->view->assign('search', $search);  //搜索条件
        $this->view->assign('list', $list);  
        $this->view->display('game/masterList');
    }

    /**
    * POI列表
    */
    public function dopoiList()
    {   
        $p = common::request('p', 'R', 1);   //页码
        $poi = common::request('poi', 'R');
        $city = common::request('city', 'R');
        $boss_type = intval(common::request('boss_type', 'R'));   
        $search = compact('poi','city','boss_type');
        $boss_type -= 1; 
        $countSql = "select count(1) from (select * from r_game_fight_record where 1=1 ";
        !empty($poi) && $countSql .= " and poi = \"{$poi}\"";
        !empty($city) && $countSql .= " and city = \"{$city}\"";
        $boss_type >= 0 && $countSql .= " and boss_type = {$boss_type}";
        $countSql .= " group by poi,address,boss_type) a";
        $num = $this->db->getOne($countSql);

        //分页
        $offset = 20;
        $parameter = "&poi=".$poi."&city=".$city."&boss_type=".$boss_type;
        require FILE . 'lib/page.class.php';
        $page = new Page($num, $offset, $parameter);
        $pageStr = $page->show();
        $start = ($p - 1) * $offset;

        $sql = "select poi,city,address,boss_type,count(uid) userCount from r_game_fight_record where 1=1 ";
        !empty($poi) && $sql .= " and poi = \"{$poi}\"";
        !empty($city) && $sql .= " and city = \"{$city}\"";
        $boss_type >= 0 && $sql .= " and boss_type = {$boss_type}";
        $sql .= " group by poi,address,boss_type order by userCount desc limit $start,$offset";

        $list = $this->db->getAll($sql);
        if($list){
            foreach ($list as &$v) {
                $v['boss_type'] == 0 && $v['typeName'] = "小怪";
                $v['boss_type'] == 1 && $v['typeName'] = "中怪";
                $v['boss_type'] == 2 && $v['typeName'] = "大怪";
            }
        }

        $this->view->assign('pageStr', $pageStr);
        $this->view->assign('search', $search);  //搜索条件
        $this->view->assign('list', $list);  
        $this->view->display('game/poiList');
    }

    /**
    * poi详情
    */
    public function dopoiDetail()
    {
        $p = common::request('p', 'R', 1);
        $poi = common::request('poi', 'R');
        $type = common::request('type', 'R');
        $boss_id = common::request('boss_id', 'R');

        if(empty($poi)){
            $_ENV['error']->getErrors(101);
            exit;
        }

        $countSql = "select count(1) from r_game_fight_record where poi = \"{$poi}\" ";
        !empty($boss_id) && $countSql .= " and boss_id = {$boss_id}";
        $num = $this->db->getOne($countSql);

        $search = compact('boss_id');
        //分页
        $offset = 20;
        $parameter = "&boss_id=".$boss_id;
        require FILE . 'lib/page.class.php';
        $page = new Page($num, $offset, $parameter);
        $pageStr = $page->show();
        $start = ($p - 1) * $offset;

        $sql = "select poi,uid,boss_id from r_game_fight_record where poi = \"{$poi}\" ";
        !empty($boss_id) && $sql .= " and boss_id = {$boss_id}";
        $sql .= " limit $start,$offset";

        $list = $this->db->getAll($sql);
        if($list){
            foreach ($list as &$v) {
                $monsterInfo = $this->db->getRow("select * from r_game_monsters where uid = {$v['uid']}");
                $v['points'] = $monsterInfo['points'];
                $v['level'] = $this->load('game')->getLevelByExperience($monsterInfo['experience']);;
            }
        }
        
        $this->view->assign('poi', $poi);
        $this->view->assign('type', $type);
        $this->view->assign('search', $search);  
        $this->view->assign('list', $list);
        $this->view->assign('pageStr', $pageStr);
        $this->view->display('game/poiDetail');
    }

    /*
     * POI管理
     */
    public function dopoiManage(){
        $p = common::request('p', 'R', 1);   //页码
        
        $countSql = "select count(1) from r_game_boss_rules";
        $num = $this->db->getOne($countSql);

        //$search = compact('giftType');
        //分页
        $offset = 20;
        //$parameter = "&giftType=".$giftType;
        require FILE . 'lib/page.class.php';
        $page = new Page($num, $offset/*, $parameter*/);
        $pageStr = $page->show();
        $start = ($p - 1) * $offset;

        $sql = "select * from r_game_boss_rules limit $start,$offset";

        $list = $this->db->getAll($sql);
        if($list){
            foreach ($list as &$v) {
                $v['boss_type'] == 0 && $v['type'] = '小怪';
                $v['boss_type'] == 1 && $v['type'] = '中怪';
                $v['boss_type'] == 2 && $v['type'] = '大怪';
                $v['attack_reduce'] = floatval($v['attack_reduce'] * 100);
            }
        }

        $this->view->assign('list',$list);
        $this->view->assign('pageStr', $pageStr);
        $this->view->display('game/poiManage');
    }

    /*
     * POI规则添加/修改
     */
    public function doaddPoiRule(){
        $id = common::request('id', 'R');

        //根据id获取奖品信息
        $pioRuleInfo = array();
        $isNew = false;
        if($id){    //update
            $isNew = true;
            $pioRuleInfo = $this->db->getRowTable('r_game_boss_rules', array(), "id = {$id}");
            $pioRuleInfo['attack_reduce'] = floatval($pioRuleInfo['attack_reduce'] * 100);
        }

        if(common::method() == 'POST')
        {   
            $name = common::request('name','R');
            $boss_type = common::request('boss_type','R');
            $img_icon = $this->load('qiniu')->uploadQiNiuImg('img_icon', md5(time().'gameBossIcon'));
            $img_icon = $img_icon ? $img_icon : common::request('keepIcon','R');
            $img_big = $this->load('qiniu')->uploadQiNiuImg('img_big', md5(time().'gameBossImage'));
            $img_big = $img_big ? $img_big : common::request('keepImgbig','R');
            $hp = common::request('hp','R');
            $attack = common::request('attack','R');
            $attack_reduce = common::request('attack_reduce','R');
            $attack_reduce = $attack_reduce / 100;
            $poi_keywords = common::request('poi_keywords','R');
            $is_show = common::request('is_show', 'P');

            $params = compact('id','name','boss_type','img_icon','img_big','hp', 'attack','attack_reduce','poi_keywords','is_show');

            if($id){   //update
                $params['id'] = $id;
                $this->db->updateRowTable('r_game_boss_rules', $params);
            }else{
                $params['addtime'] = time();
                $this->db->insertRowTable('r_game_boss_rules', $params);
            }
            common::go("http://".$this->config['host'].'/adminGame/poiManage');
        }else{
            !$pioRuleInfo && $pioRuleInfo = array('id' => '', 'name' => '', 'boss_type' => '', 'img_icon' => '', 'img_big' => '', 'hp' => '', 'attack' => '', 'attack_reduce' => '', 'poi_keywords' => '', 'is_show' => 1);

            $this->view->assign('isNew', $isNew);
            $this->view->assign('pioRuleInfo', $pioRuleInfo);
            $this->view->display('game/addPoiRule');
        }
    }

    #########################数据统计########################

    /*
     * 通用数据
     */
    public function docommonData(){
        $commonArr = array('userCount' => 0, 'unknow' => 0, 'manCount' => 0, 'womanCount' => 0, 'avgAge' => 0, 'produceEnergy' => 0, 'expendEnergy' => 0);
        $countSql = "select count(1) from r_game_users";
        $commonArr['userCount'] = $this->db->getOne($countSql);
        $genderSql = "SELECT gender,count(gender) genderCount from r_user where uid in (SELECT uid from r_game_users) GROUP BY gender";
        $genderArr = $this->db->getAll($genderSql);
        if($genderArr){
            foreach ($genderArr as &$v) {
                $v['gender'] == 0 && $commonArr['unknow'] = $v['genderCount'];
                $v['gender'] == 1 && $commonArr['manCount'] = $v['genderCount'];
                $v['gender'] == 2 && $commonArr['womanCount'] = $v['genderCount'];
            }
        }
        $ageSql = "SELECT avg(DATE_FORMAT(NOW(), '%Y') - age) avgage FROM r_user where uid in (SELECT uid from r_game_users)";
        $commonArr['avgAge'] = $this->db->getOne($ageSql);
        $commonArr['avgAge'] = number_format($commonArr['avgAge'], 2);
        $energySql = "SELECT type, sum(energy_add) totalEnergy FROM r_energy_record where type in (\"careMonster\",\"gameGift\") and uid in (SELECT uid from r_game_users) GROUP BY type";
        $energyArr = $this->db->getAll($energySql);
        if($energyArr){
            foreach ($energyArr as &$v) {
                $v['type'] == 'careMonster' && $commonArr['expendEnergy'] = -$v['totalEnergy'];
                $v['type'] == 'gameGift' && $commonArr['produceEnergy'] = $v['totalEnergy'];
            }
        }

        $this->view->assign('commonArr', $commonArr);
        $this->view->display('game/commonData');
    }

    /*
     * 年龄分布
     */
    public function doageDistribution(){
        $ageData = array('lessthan16' => 0, 'in17and22' => 0, 'in23and28' => 0, 'in29and34' => 0, 'in35and40' => 0, 'in41and50' => 0, 'greaterthan50' => 0);
        $lessthan16Sql = "SELECT count(uid) FROM r_user where DATE_FORMAT(NOW(), '%Y') - age <= 16 and uid in (SELECT uid from r_game_users) ";
        $ageData['lessthan16'] = $this->db->getOne($lessthan16Sql);

        $in17and22Sql = "SELECT count(uid) FROM r_user where DATE_FORMAT(NOW(), '%Y') - age between 17 and 22 and uid in (SELECT uid from r_game_users)";
        $ageData['in17and22'] = $this->db->getOne($in17and22Sql);

        $in23and28Sql = "SELECT count(uid) FROM r_user where DATE_FORMAT(NOW(), '%Y') - age between 23 and 28 and uid in (SELECT uid from r_game_users)";
        $ageData['in23and28'] = $this->db->getOne($in23and28Sql);

        $in29and34Sql = "SELECT count(uid) FROM r_user where DATE_FORMAT(NOW(), '%Y') - age between 29 and 34 and uid in (SELECT uid from r_game_users)";
        $ageData['in29and34'] = $this->db->getOne($in29and34Sql);

        $in35and40Sql = "SELECT count(uid) FROM r_user where DATE_FORMAT(NOW(), '%Y') - age between 35 and 40 and uid in (SELECT uid from r_game_users)";
        $ageData['in35and40'] = $this->db->getOne($in35and40Sql);

        $in41and50Sql = "SELECT count(uid) FROM r_user where DATE_FORMAT(NOW(), '%Y') - age between 41 and 50 and uid in (SELECT uid from r_game_users)";
        $ageData['in41and50'] = $this->db->getOne($in41and50Sql);

        $greaterthan50Sql = "SELECT count(uid) FROM r_user where DATE_FORMAT(NOW(), '%Y') - age >= 50 and uid in (SELECT uid from r_game_users)";
        $ageData['greaterthan50'] = $this->db->getOne($greaterthan50Sql);

        $this->view->assign('ageData', $ageData);
        $this->view->display('game/ageDistribution');
    }

    /*
     * 跑量分布
     */
    public function dorunDistribution(){
        $levelArr = common::$runLevel;
        $runData = array();
        foreach ($levelArr as $k => $val) {
            $runData[$k]['levelName'] = $val['title'];
            $runData[$k]['minMileage'] = $val['min'];
            if($k < count($levelArr)){
                $maxMileage = $levelArr[$k+1]['min'];
                $sql = "SELECT count(uid) userCount FROM r_game_users where run_mileage between {$val['min']} and {$maxMileage}";
            }else{
                $sql = "SELECT count(uid) userCount FROM r_game_users where run_mileage >= {$val['min']}";
            }
            $runData[$k]['userCount'] = $this->db->getOne($sql);
        }

        if($runData){
            foreach ($runData as $key => $v) {
                if($v['userCount'] == 0){
                    unset($runData[$key]);
                }
            }
        }

        $this->view->assign('runData', $runData);
        $this->view->display('game/runDistribution');
    }

    /*
     * 拥有勋章分布
     */
    public function domedalDistribution(){
        $medalData = array('lessthan5' => 0, 'in5and10' => 0, 'greaterthan10' => 0);
        //获取游戏开始时间
        $gameInit = $this->redis->get(REDIS_PREFIX . 'gameIsOpen');
        if(!$gameInit){
            return $medalData;
        }
        $gameInit = json_decode($gameInit, true);
        $gameStart = strtotime($gameInit['start_time']);

        $lessthan5Sql = "SELECT count(1) FROM (SELECT r.uid,sum(r.number) medalCount FROM r_medal m LEFT JOIN r_medal_record r ON m.medal_id = r.medal_id WHERE m.cate = 2 and r.add_time > {$gameStart} and r.uid in (SELECT uid from r_game_users) GROUP BY r.uid HAVING sum(r.number) < 5) a";
        $medalData['lessthan5'] = $this->db->getOne($lessthan5Sql);

        $in5and10Sql = "SELECT count(1) FROM (SELECT r.uid,sum(r.number) medalCount FROM r_medal m LEFT JOIN r_medal_record r ON m.medal_id = r.medal_id WHERE m.cate = 2 and r.add_time > {$gameStart} and r.uid in (SELECT uid from r_game_users) GROUP BY r.uid HAVING sum(r.number) between 5 and 10) a";
        $medalData['in5and10'] = $this->db->getOne($in5and10Sql);

        $greaterthan10Sql = "SELECT count(1) FROM (SELECT r.uid,sum(r.number) medalCount FROM r_medal m LEFT JOIN r_medal_record r ON m.medal_id = r.medal_id WHERE m.cate = 2 and r.add_time > {$gameStart} and r.uid in (SELECT uid from r_game_users) GROUP BY r.uid HAVING sum(r.number) > 10) a";
        $medalData['greaterthan10'] = $this->db->getOne($greaterthan10Sql);

        $this->view->assign('medalData', $medalData);
        $this->view->display('game/medalDistribution');
    }

    /*
     * 注册时长分布
     */
    public function doregistDistribution(){
        $registData = array('lessthan1' => 0, 'in1and6' => 0, 'in6and12' => 0, 'greaterthan12' => 0);

        $lessthan1Sql = "SELECT COUNT(1) FROM (SELECT TIMESTAMPDIFF(MONTH, from_unixtime(addtime), NOW()) timeDiff FROM r_user where uid in (SELECT uid from r_game_users) GROUP BY uid HAVING timeDiff < 1) a";
        $registData['lessthan1'] = $this->db->getOne($lessthan1Sql);

        $in1and6Sql = "SELECT COUNT(1) FROM (SELECT TIMESTAMPDIFF(MONTH, from_unixtime(addtime), NOW()) timeDiff FROM r_user where uid in (SELECT uid from r_game_users) GROUP BY uid HAVING timeDiff between 1 and 6) a";
        $registData['in1and6'] = $this->db->getOne($in1and6Sql);

        $in6and12Sql = "SELECT COUNT(1) FROM (SELECT TIMESTAMPDIFF(MONTH, from_unixtime(addtime), NOW()) timeDiff FROM r_user where uid in (SELECT uid from r_game_users) GROUP BY uid HAVING timeDiff between 7 and 12) a";
        $registData['in6and12'] = $this->db->getOne($in6and12Sql);

        $greaterthan12Sql = "SELECT COUNT(1) FROM (SELECT TIMESTAMPDIFF(MONTH, from_unixtime(addtime), NOW()) timeDiff FROM r_user where uid in (SELECT uid from r_game_users) GROUP BY uid HAVING timeDiff > 12) a";
        $registData['greaterthan12'] = $this->db->getOne($greaterthan12Sql);

        $this->view->assign('registData', $registData);
        $this->view->display('game/registDistribution');
    }

    /*
     * 正能量变动分布
     */
    public function doenergyDistribution(){
        $energyData = array();
        $sql = "SELECT date,sum(energy_add) energy FROM r_energy_record WHERE type in (\"careMonster\",\"gameGift\") GROUP BY date,type";
        $energyArr = $this->db->getAll($sql);
        if($energyArr){
            foreach ($energyArr as $k => $v) {
                $energyData[$v['date']]['date'] = $v['date'];
                $v['energy'] > 0 && $energyData[$v['date']]['addEnergy'] = $v['energy'];
                $v['energy'] < 0 && $energyData[$v['date']]['expendEnergy'] = -$v['energy'];
            }
        }
        if($energyData){
            foreach ($energyData as $key => $val) {
                !isset($energyData[$key]['addEnergy']) && $energyData[$key]['addEnergy'] = 0;
                !isset($energyData[$key]['expendEnergy']) && $energyData[$key]['expendEnergy'] = 0;
            }
        }
        $this->view->assign('energyData', $energyData);
        $this->view->display('game/energyDistribution');
    }

    /*
     * 游戏数据
     */
    public function dogameData(){
        $gameInfo = $this->redis->hgetall(REDIS_PREFIX.'gameDataByDate');
        $gameData = array();
        
        if(!empty($gameInfo)){
            foreach ($gameInfo as $key => $val) {
                $gameData[$key] = json_decode($val, true);
            }
        } 

        $this->view->assign('gameData', $gameData);
        $this->view->display('game/gameData');
    }
}